#include"MyString.h"
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
// #define true 1
// #define false 2


void Initialize(MyString *obj,const char *str);


void Print(MyString *obj);
int Size(MyString *obj);
int IsEqual(MyString *obj1,MyString *obj2);
int IsContains(MyString*dest,MyString*src);
char* AtIndex(MyString *obj,int index);
void RemoveString(MyString *obj,const char *str);
int InsertString(MyString *obj,const char *str,int index);

void Initialize(MyString *obj, const char *str)
{
    obj->size=strlen(str);

    obj->cubage=obj->size+1;
    obj->string=(char *)malloc(obj->cubage);
    if(obj->string==NULL)
    {
        printf("malloc error!");
        return false;
    }
    strcpy(obj->string,str);

    obj->print=Print;
    obj->isEqual=IsEqual;
    obj->atIndex=AtIndex;
    obj->insertString=InsertString;
    obj->isContains=IsContains;
    obj->Size=Size;
    obj->removeString=RemoveString;    
    // obj->cubage=Size+1;

}

void FreeString(MyString *obj)
{
    if(obj->string!=NULL)
    {
        free(obj->string);
        obj->string=NULL;
        obj->size=0;
        obj->cubage=0;
    }
}

// int ReallocArray(MyString *obj)
// {
//     char *temp=obj->string;
//     obj->cubage=obj->cubage*2;
//     obj->string=(char *)malloc(sizeof(char)*obj->cubage);
//     if(obj->string==NULL)
//     {
//         printf("ReallocArray error!\n");
//         obj->string=temp;
//         obj->cubage=obj->cubage/2;
//         return false;
//     }

//     strcpy(obj->string,temp);

//     free(temp);
//     return true;
// }

void Print(MyString *obj)
{
    printf("string cubage:%d string size:%d\n",obj->cubage,obj->size);
    printf("====================================\n");
    printf("%s\n",obj->string);
    printf("====================================\n");

}

int Size(MyString *obj)
{
    return obj->size;
}

int IsEqual(MyString *obj1, MyString *obj2)
{
    if(strcmp(obj1->string,obj2->string)==0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int IsContains(MyString *dest, MyString *src)
{
    char *equal=strstr(dest->string,src->string);
    if(equal==NULL)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

char* AtIndex(MyString *obj, int index)
{
    if(index>=obj->size || index<0)
    {
        return NULL;
    }
    char *ptr=obj->string;
    while(index!=0)
    {
        ptr++;
        index--;
    }
    return ptr;

}

void RemoveString(MyString *obj, const char *str)
{
    char *start=strstr(obj->string,str);
    if(start==NULL)
    {
        return;
    }
    else
    {
        char*back=start+strlen(str);
        while(*back!='\0')
        {
            *start=*back;
            start++;
            back++;
        }
        *start='\0';
        obj->size=strlen(obj->string);
    }

}

int InsertString(MyString *obj, const char *str, int index)
{
    if(index<0||index>obj->size)
    {
        printf("invalid place!\n");
        return false;
    }
    int new_need_len=obj->size+strlen(str)+1;
    char *new_str=(char *)malloc(sizeof(char)*new_need_len);
    if(new_str==NULL)
    {
        printf("can not contain more char!\n");
        return false;
    }


    strncpy(new_str,obj->string,index);
    strncpy(new_str+index,str,strlen(str));
    strcpy(new_str+index+strlen(str),obj->string+index);
    // memset(obj->string,0,Max);
    // strcpy(obj->string,new_str);
    free(obj->string);
    obj->string=new_str;

    if(new_need_len>obj->cubage)
        obj->cubage=new_need_len;

    obj->size=strlen(obj->string);
}



